package com.xcf.controller.base;


import com.github.pagehelper.PageInfo;
import com.xcf.entity.Page;
import com.xcf.entity.system.User;
import com.xcf.util.Const;
import com.xcf.util.Logger;
import com.xcf.util.PageData;
import com.xcf.util.UuidUtil;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Micro Chen
 * @创建时间：2017.09.25
 */
public class BaseController {
	
	protected Logger logger = Logger.getLogger(this.getClass());
    protected org.slf4j.Logger log = LoggerFactory.getLogger(getClass());
	private static final long serialVersionUID = 6357869213649815390L;
	
	/** new PageData对象
	 * @return
	 */
	public PageData getPageData(){
		return new PageData(this.getRequest());
	}

	public User getLoginUser(HttpServletRequest request, HttpServletResponse res){
		User user = (User)request.getSession().getAttribute(Const.SESSION_USER);
		if(user == null){
			try {
				res.sendRedirect("/login_toLogin");
			}catch (Exception ex){

			}
		}
		return user;
	}
	/**得到ModelAndView
	 * @return
	 */
	public ModelAndView getModelAndView(){
		return new ModelAndView();
	}
	
	/**得到request对象
	 * @return
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	/**得到32位的uuid
	 * @return
	 */
	public String get32UUID(){
		return UuidUtil.get32UUID();
	}
	
	/**得到分页列表的信息
	 * @return
	 */
	public Page getPage(){
		return new Page();
	}
	
	public static void logBefore(Logger logger, String interfaceName){
		//logger.info("");
		logger.info("======start============");
		logger.info(interfaceName);
	}
	
	public static void logAfter(Logger logger){
		logger.info("======end==============");
		//logger.info("");
	}

	/***
	 *
	 * @param request
	 * @return
     */
	public Map getParams(HttpServletRequest request){
		Map properties = request.getParameterMap();
		Map returnMap = new HashMap();
		Iterator entries = properties.entrySet().iterator();
		Map.Entry entry;
		String name = "";
		String value = "";
		while (entries.hasNext()) {
			entry = (Map.Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if(null == valueObj){
				value = "";
			}else if(valueObj instanceof String[]){
				String[] values = (String[])valueObj;
				for(int i=0;i<values.length;i++){
					value = values[i] + ",";
				}
				value = value.substring(0, value.length()-1);
			}else{
				value = valueObj.toString();
			}
			returnMap.put(name, value);
		}
		return returnMap;
	}

	public void handlePageParams(Map param,Page page){
		if(param==null){
			return;
		}
		if(param.containsKey("page.currentPage")){
			page.setCurrentPage(Integer.valueOf(""+param.get("page.currentPage")));
		}
		if(param.containsKey("page.showCount")){
			page.setShowCount(Integer.valueOf(""+param.get("page.showCount")));
		}
		if(param.containsKey("start")&&param.containsKey("limit")){
			page.setCurrentPage(Integer.valueOf(param.get("start")+""));
			page.setShowCount(Integer.valueOf(param.get("limit")+""));
		}

	}

	public void handlePage(Page page, PageInfo d){
		d.setPageSize(page.getShowCount());
		d.setPageNum(page.getCurrentPage());
		d.setPages(page.getTotalPage());
		d.setTotal(page.getTotalResult());
	}

	class ExceptionHandler{

	}
}
